<!-- 商和采购订单模块 -->
<template>
  <div>
    <el-header v-if="urlData.up === '0' && getUrlData.workItemId" height="36px">
      <header-title :title="urlData.titles" />
    </el-header>
    <TCollapse
      title="采购订单调整情况"
      :router-path="'purchaseOrder'"
      :url-data="getUrlData"
      :is-show-fuscreen="urlData.up === '0'"
    >
      <!-- 消息提示 -->
      <MessageTitle :show-title="showTitle" :message-list="messageList" />
      <div style="display: flex;align-items: center;color: #ff0000;line-height: 18px">（4）根据&lt;供应链管理部&gt;要求只允许塔、房和电源等设备退至虚拟库（即退库类型允许选择可用不入库或不可用不入库），其余物资不允许退至虚拟库。
            <el-button class="pms-button-primary" @click="clickMaterialClassify">可退至虚拟库物料分类</el-button></div>
      <!-- 筛选条件 只有节点是sett_01 才可以展示-->
      <FilterCriteria
        v-if="isShowBtns"
        ref="myFilter"
        :url-data="getUrlData"
        @upPurchaseOrder="upPurchaseOrder"
        @cancelPurchaseOrder="cancelPurchaseOrder"
      />
      <!-- 切换选项卡 -->
      <SwitchTabs
        v-if="isShowSwitchTabs"
        ref="myTab"
        :params-obj="paramsObj"
        :url-data="urlData"
        @nextBtnDilogShow="nextBtnDilogShow"
        v-bind="$attrs"
      />
      <el-dialog
        ref="materialClassification"
        v-dialogDrag
        :close-on-click-modal="false"
        :visible.sync="showMaterialClassification"
        width="70%"
      >
        <div slot="title" class="dialog-header">可退至虚拟库物料分类</div>
        <material-classification
          v-if="showMaterialClassification"
          ref="cancelRetreatVisible"
        />
      </el-dialog>
    </TCollapse>
  </div>
</template>

<script>
import HeaderTitle from '@/components/basic/HeaderTitle'
import TCollapse from '../components/TCollapse.vue'
import MessageTitle from '../components/MessageTitle.vue'
import FilterCriteria from './FilterCriteria.vue'
import SwitchTabs from './SwitchTabs.vue'
import { getQueryObject } from '@/utils'
import { messageList } from '../index.js'
import {
  updateBusinessOrder,
  cancelUpdateBusinessOrder,
  getBalanceBasicDetails
} from '@/api/settlement/balanceSettlement.js'
import {
  getWFWorkItem
} from '@/api/actualBudget/index'
import materialClassification from '@/views/material/components/materialClassification.vue'
export default {
  name: 'PurchaseOrder',
  components: {
      materialClassification,
    TCollapse,
    MessageTitle,
    FilterCriteria,
    SwitchTabs,
    HeaderTitle
  },
  props: {
    paramsObj: {
      type: Object,
      default: () => ({ })
    }
  },
  data() {
    return {
      // 是否显示标题提示
      showTitle: true,
      // 从全屏跳转过来需要用的url地址数据
      urlData: {
      },
      messageList, // 消息提示列表
      isShowBtns: false, // 是否显示商合按钮 只有节点sett_01 展示
      isShowSwitchTabs: true, // 是否显示切换选项卡组件
      showMaterialClassification: false
    }
  },
  computed: {
    // 重要 判断是否是全屏来的 还是 默认进来的 浏览器地址
    getUrlData() {
      return this.paramsObj?.adjustId !== undefined ? this.paramsObj : this.urlData
    }
  },
  created() {
    // 获取浏览器地址参数
    this.initBrowserAddressParams()
    // 初始化为了显示是否更新商合订单按钮
    this.initJudgeUpdate()
    // 初始化是否显示更新商合按钮
    this.initIsShowBtns()
  },
  methods: {
    childrenSave() {
      const materiaList = this.$refs.myTab?.tableOption.tablDataMaterial
      const servierList = this.$refs.myTab?.tableOption.tablDataService
      const _activeName = this.$refs.myTab?.activeName
      return {
        materiaList,
        servierList,
        _activeName
      }
    },
    clickMaterialClassify() {
      this.showMaterialClassification = true
    },
    // 下一步保存校验
    nextSaveValidate() {
      this.$refs.myTab.nextIsEditClick()
    },
    // 是否让下一步弹框显示
    nextBtnDilogShow(tag) {
      this.$emit('showBtnNext', tag)
    },
    // 初始化获取浏览器地址参数
    initBrowserAddressParams() {
      const obj = getQueryObject()
      this.urlData = obj
    },
    // 更新商合按钮接口
    async upPurchaseOrder() {
      const { prjId, adjustId, processInstId } = this.getUrlData
      const { code } = await updateBusinessOrder(prjId, adjustId, '5', processInstId)
      if (+code === 200) {
        this.isShowSwitchTabs = true
        this.$message.success('更新商合采购订单成功')
        // 更新成功以后调用查看当前状态
        this.initJudgeUpdate()
      }
    },
    // 取消更新商合采购订单
    async cancelPurchaseOrder() {
      this.$confirm('取消采购订单会清空物资与服务数据，是否继续？', '系统提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
          cancelButtonClass: 'pms-zr-button',
          confirmButtonClass: 'pms-zr-button-primary',
        type: 'warning'
      }).then(async() => {
        const { prjId, adjustId, processInstId } = this.getUrlData
        const { code } = await cancelUpdateBusinessOrder(prjId, adjustId, processInstId)
        if (+code === 200) {
          this.isShowSwitchTabs = false
          this.initJudgeUpdate()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }
      }).catch(() => {
        this.initJudgeUpdate()
        // this.$message({
        //   type: 'info',
        //   message: '已取消删除'
        // })
      })
    },
    // 初始化为了显示是否更新商合订单按钮
    async initJudgeUpdate() {
      const { adjustId } = this.getUrlData
      if (!adjustId) {
        return
      }
      // 获取结算当前基本信息
      const { code, data } = await getBalanceBasicDetails(adjustId)
      if (+code === 200) {
        // 是否更新商合订单 0：否 1是
        let type = 1
        if (+data?.isUpdateOrder === 0) {
          type = 0
          this.isShowSwitchTabs = false
        } else {
          type = 1
          this.isShowSwitchTabs = true
        }
        if (this.$refs.myFilter !== undefined) {
          this.$refs.myFilter.changeValue(type)
        }
      }
    },
    // 初始化是否显示更新/取消商合按钮
    async initIsShowBtns() {
      const { workItemId, type } = this.getUrlData
      // 没有workItemId 综合查询进来的 需要隐藏更新商合按钮
      if (!workItemId || type === 'done') {
        this.isShowBtns = false
        return
      }
      const { code, data } = await getWFWorkItem({ workItemId: workItemId })
      if (+code === 200) {
        const { activityDefID } = data
        if (activityDefID === 'sett_01') {
          this.isShowBtns = true
        }
      }
    }
  }
}
</script>
<style lang="scss" scoped></style>
